import $ from 'jquery';
import { renderHome } from './pages/home';
import { renderAbout } from './pages/about';
import { renderContact } from './pages/contact';

// 路由配置
const routes = {
  '/': renderHome,
  '/about': renderAbout,
  '/contact': renderContact
};

// 渲染页面内容
function renderContent(path) {
  const render = routes[path] || routes['/'];
  $('#app').html(render());
}

// 初始化路由
export default function initRouter() {
  // 处理导航点击事件
  $(document).on('click', '[data-route]', function(e) {
    e.preventDefault();
    const path = $(this).data('route');
    history.pushState(null, '', path);
    renderContent(path);
  });

  // 处理浏览器前进/后退
  $(window).on('popstate', () => {
    renderContent(location.pathname);
  });

  // 初始渲染
  renderContent(location.pathname);
}